home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / EleOfC++ vrs 0.1 / TextEdit / TTEDoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  6.8 KB  |  432 lines  |  [TEXT/KAHL]

  1. /*
  2. ** This is the TTEDoc class definition.  This class is an adaptation of 
  3. ** the class of the same name in the book, Elements of C++ Macintosh 
  4. ** Programming, by Dan Weston.
  5. **
  6. ** Copyright © 1991 Mark Gross 
  7. ** (RR2, Box 84, Clayville, NY 13322);         
  8. ** this file may be published everywhere, but no charge
  9. ** may be made for its use.  Please contact me about any bugs found.
  10. ** I'm also looking for Macintosh development work, so drop me a line
  11. ** if you think I could help.  
  12. */
  13.  
  14. #include "TTEDoc.h"
  15. #include "magics.h"
  16.  
  17. TDoc*    TTEDoc::Init( OSType theCreator, SFReply *SFInfo)
  18. {
  19.     inherited::Init(theCreator, SFInfo);
  20.     fTEHandle = NULL;
  21. }/*end of function*/
  22.  
  23.  
  24. void    TTEDoc::Delet( void)
  25. {
  26.     if (fTEHandle != NULL)
  27.     {
  28.         TEDispose(fTEHandle);
  29.         fTEHandle = NULL;
  30.     }
  31. }/*end of function*/
  32.  
  33.     
  34. Boolean        TTEDoc::InitDoc( void)
  35. {
  36.     Rect    view, dest;
  37.     
  38.     
  39.     
  40.     
  41.     if (TScrollDoc::InitDoc() )
  42.     {
  43.         SetPort(fDocWindow);
  44.         
  45.         view = dest = fDocWindow->portRect;
  46.         dest.left += kTEMargin;
  47.         dest.top += kTEMargin;
  48.         dest.right = kMaxShort;
  49.         dest.bottom = kMaxShort;
  50.         
  51.         fTEHandle = TENew(&dest, &view);
  52.         SetTERect();
  53.         
  54.         TEAutoView(TRUE, fTEHandle);
  55.         
  56.         SetClikLoop(MyClickLoop, fTEHandle);
  57.     }/*end if*/
  58.     
  59.     return (fTEHandle != NULL);
  60. }/*end of function*/
  61.  
  62.         
  63. void    TTEDoc::ScrollContents( short dh, short dv)
  64. {
  65.     if(fTEHandle != NULL)
  66.         TEScroll(dh,dv,fTEHandle);
  67. }/* end of function*/
  68.  
  69.  
  70. void    TTEDoc::SetScrollBarValues( void )
  71. {
  72.     Rect    visible, dest;
  73.     short    vPos, hPos;
  74.     
  75.     visible = (**fTEHandle).viewRect;
  76.     dest = (**fTEHandle).destRect;
  77.     
  78.     vPos = visible.top - dest.top;
  79.     hPos = visible.left - dest.left;
  80.     FocusOnWindow();
  81.     SetCtlValue(fHorizScrollBar, hPos);
  82.     SetCtlValue(fVertScrollBar, vPos);
  83.     
  84. }/*end of function*/
  85.  
  86.  
  87.  
  88. void    TTEDoc::GetContentRect( Rect *r)
  89. {
  90.     inherited::GetContentRect(r);
  91.     
  92.     r->left += kTEMargin;
  93.     r->top += kTEMargin;
  94. }/*end of function*/
  95.  
  96.  
  97.  
  98. void    TTEDoc::SetTERect( void)
  99. {
  100.     Rect r;
  101.     
  102.     if(fTEHandle != NULL)
  103.     {
  104.         GetContentRect(&r);
  105.         (**fTEHandle).viewRect = r;
  106.     }
  107. }/*end of function*/
  108.  
  109.         
  110.  
  111.  
  112. short    TTEDoc::GetVertSize(void)
  113. {
  114.     return ( (**fTEHandle).nLines * (**fTEHandle).lineHeight );
  115. }
  116.  
  117.  
  118. short    TTEDoc::GetHorizSize(void)
  119. {
  120.     return ( (**fTEHandle).destRect.right - (**fTEHandle).destRect.left );
  121. }
  122.  
  123.  
  124.  
  125. short     TTEDoc::GetVertLineScrollAmount(void)
  126. {
  127.     if(fTEHandle != NULL)
  128.         return (**fTEHandle).lineHeight;
  129.     else
  130.         return 0;
  131. }/*end of function*/
  132.  
  133.  
  134. short    TTEDoc::GetHorizLineScrollAmount(void)
  135. {
  136.     if( fTEHandle != NULL)
  137.         return (**fTEHandle).lineHeight;
  138.     else
  139.         return 0;
  140. }/*end of function*/
  141.  
  142.     
  143. Boolean    TTEDoc::CanSelectAll(void)
  144. {
  145.     return TRUE;
  146. }
  147.  
  148. void    TTEDoc::DoSelectAll(void)
  149. {
  150.     if(fTEHandle != NULL)
  151.         TESetSelect(0, kMaxShort, fTEHandle);
  152. }/*end of function*/
  153.  
  154.  
  155. Boolean    TTEDoc::HaveSelection(void)
  156. {
  157.     if (fTEHandle != NULL)
  158.         return ( (**fTEHandle).selStart != (**fTEHandle).selEnd);
  159.     else
  160.         return FALSE;
  161. }/* end of function*/
  162.  
  163.  
  164.  
  165. void    TTEDoc::DoKeyDown(EventRecord *theEvent)
  166. {
  167.     if(fTEHandle)
  168.     {
  169.         TEKey(LoWord(theEvent->message), fTEHandle);
  170.         fNeedtoSave = TRUE;
  171.         SynchScrollBars();
  172.     }
  173.  
  174. }/*end of function*/
  175.  
  176.  
  177.  
  178. void    TTEDoc::AddText(Ptr text, long len)
  179. {
  180.     if (fTEHandle)
  181.     {
  182.         TEInsert(text, len, fTEHandle);
  183.         fNeedtoSave = TRUE;
  184.         TESelView(fTEHandle);
  185.         SynchScrollBars();
  186.     }
  187.  
  188. }/*end of function*/
  189.  
  190.     
  191. Boolean    TTEDoc::CanPaste(OSType theType)
  192. {
  193.     return (theType == 'TEXT');
  194. }
  195.  
  196.  
  197. void    TTEDoc::DoClear(void)
  198. {
  199.     if(fTEHandle)
  200.     {
  201.         TEDelete(fTEHandle);
  202.         fNeedtoSave = TRUE;
  203.         SynchScrollBars();
  204.     }
  205.     
  206. }/*end of function*/
  207.  
  208.  
  209.  
  210. Boolean    TTEDoc::DoCopy(Handle *theData, OSType *theType)
  211. {
  212.     Handle TEData;
  213.     OSErr err;
  214.     
  215.  
  216.     if (fTEHandle)
  217.     {
  218.         TECopy(fTEHandle);
  219.         *theType = 'TEXT';
  220.         *theData = NULL;
  221.         TEData = TEScrapHandle();
  222.         err = HandToHand(&TEData);
  223.         if(err != noErr)
  224.             return FALSE;
  225.         
  226.         *theData = TEData;
  227.         
  228.         return TRUE;
  229.     }
  230. }/*end of function*/
  231.  
  232.         
  233.  
  234. Boolean    TTEDoc::DoCut(Handle *theData, OSType *theType)
  235. {
  236.     Boolean result;
  237.     
  238.     if(result = DoCopy(theData, theType) )
  239.         DoClear();
  240.     
  241.     return result;
  242. }/*end of function*/
  243.  
  244.  
  245. void    TTEDoc::DoPaste(Handle theData, OSType *theType)
  246. {
  247.     Handle    TEScrapHandle;
  248.     long    scrapLen;
  249.     
  250.     if( (fTEHandle) && (*theType == 'TEXT') )
  251.     {
  252.         scrapLen = GetHandleSize(theData);
  253.         TESetScrapLen(scrapLen);
  254.         TEScrapHandle = (Handle ) TEScrpHandle;
  255.         TEScrapHandle = theData;
  256.         TEPaste(fTEHandle);
  257.         fNeedtoSave = TRUE;
  258.         SynchScrollBars();
  259.     }
  260. }/*end of function*/
  261.  
  262.     
  263. void    TTEDoc::Activate(void)
  264. {
  265.     inherited::Activate();
  266.     if(fTEHandle)
  267.         TEActivate(fTEHandle);
  268. }/*end of function*/
  269.  
  270.  
  271. void    TTEDoc::Deactivate(void)
  272. {
  273.     inherited::Deactivate();
  274.     if(fTEHandle)
  275.         TEDeactivate(fTEHandle);
  276. }/*end of function*/
  277.  
  278.     
  279. void    TTEDoc::Draw(Rect *r)
  280. {
  281.     if(fTEHandle)
  282.     {
  283.         EraseRect(r);
  284.         TEUpdate(r, fTEHandle);
  285.     }
  286. }/*end of function*/
  287.  
  288.     
  289. void    TTEDoc::ContentClick(EventRecord *theEvent)
  290. {
  291.     Boolean    shiftKeyDown;
  292.     
  293.     shiftKeyDown = ( (theEvent->modifiers & shiftKey) !=0);
  294.     if(fTEHandle)
  295.     {
  296.         TEAutoView(FALSE, fTEHandle);
  297.         TEClick(theEvent->where, shiftKeyDown, fTEHandle);
  298.         TEAutoView(TRUE, fTEHandle);
  299.     }
  300. }/*end of function*/
  301.  
  302.     
  303. void    TTEDoc::DoIdle(void)
  304. {
  305.     GrafPtr    oldPort;
  306.     Point    thePt;
  307.  
  308.     inherited::DoIdle();
  309.     if(fTEHandle)
  310.         TEIdle(fTEHandle);
  311.         
  312.     GetPort(&oldPort);
  313.     SetPort(fDocWindow);
  314.     GetMouse(&thePt);
  315.     AdjustCursor(thePt);
  316.     SetPort(oldPort);
  317. }/*end of function*/
  318.  
  319.  
  320.  
  321. void    TTEDoc::AdjustCursor(Point where)
  322. {
  323.     Rect    r;
  324.     CursHandle    IBeam;
  325.     
  326.     GetContentRect(&r);
  327.     if( PtInRect(where, &r) )
  328.     {
  329.         IBeam = GetCursor(iBeamCursor);
  330.         if(IBeam != NULL)
  331.             SetCursor(*IBeam);
  332.         
  333.     }
  334.     else
  335.         InitCursor();
  336. }/*end of function*/
  337.  
  338.  
  339.  
  340. void    TTEDoc::DoGrow(EventRecord *theEvent)
  341. {
  342.     inherited::DoGrow(theEvent);
  343.     SetTERect();
  344. }
  345.  
  346.  
  347. void    TTEDoc::DoZoom(short partCode)
  348. {
  349.     inherited::DoZoom(partCode);
  350.     SetTERect();
  351. }
  352.  
  353.     
  354. OSType    TTEDoc::GetDocType(void)
  355. {
  356.     return 'TEXT';
  357. }
  358.  
  359. Boolean TTEDoc::CanSaveAs(void)
  360. {
  361.     return TRUE;
  362. }
  363.  
  364.  
  365. Boolean    TTEDoc::ReadDocFile(short refNum)
  366. {
  367.     long    len;
  368.     OSErr    err;
  369.     Handle    thetext;
  370.     
  371.     if( (fDocWindow) && (fTEHandle != NULL) )
  372.     {
  373.             err = GetEOF(refNum, &len);
  374.             if(len > kMaxShort)
  375.                 len = kMaxShort;
  376.         
  377.         thetext = NewHandle(len);
  378.         if(thetext == NULL)
  379.         {
  380.             ErrorAlert(rDocErrorStrings, sNoMem);
  381.             return FALSE;
  382.         }
  383.         
  384.         HLock(thetext);
  385.         err = SetFPos(refNum, fsFromStart, 0);
  386.         err = FSRead(refNum, &len, (Ptr) *thetext);
  387.         HUnlock(thetext);
  388.         if(err == noErr)
  389.         {
  390.             HLock(thetext);
  391.             TESetText(*thetext, len, fTEHandle);
  392.             HUnlock(thetext);
  393.             DisposHandle(thetext);
  394.             
  395.             TESetSelect(0, 0, fTEHandle);
  396.             SynchScrollBars();
  397.             return TRUE;
  398.         }
  399.         else
  400.         {
  401.             DisposHandle(thetext);
  402.             return FALSE;
  403.         }
  404.     }/*end window*/
  405.     
  406.     return FALSE;
  407. }/*end of function*/
  408.  
  409.  
  410.  
  411. Boolean    TTEDoc::WriteDocFile(short refNum)
  412. {
  413.     long    len;
  414.     CharsHandle thetext;
  415.     OSErr    err;
  416.     
  417.     if( (fDocWindow != NULL) && (fTEHandle != NULL) )
  418.     {
  419.         len = (long) (**fTEHandle).teLength;
  420.         thetext = TEGetText(fTEHandle);
  421.         HLock( (Handle) thetext);
  422.         err = SetFPos(refNum, fsFromStart, 0);
  423.         err = FSWrite(refNum, &len, (Ptr) *thetext);
  424.         HUnlock( (Handle) thetext);
  425.         if( err == noErr )
  426.             return TRUE;
  427.         else
  428.             return    FALSE;
  429.     }
  430.     return    FALSE;
  431. }/*end of function*/
  432.